package com.app.mapper;

import com.app.model.AdminUserModel;
import com.app.model.OrderModel;
import com.app.vo.OrderStatusCountsVO;
import com.github.yulichang.base.MPJBaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 订单
 *
 * @author 16693103420
 * @since 2024-01-31
 */
@Mapper
public interface OrderMapper extends MPJBaseMapper<OrderModel> {

    @Select("select IFNULL(SUM(voucher),0)  as 'sumIncome' from tb_order where user_id = #{userId} and business_id=#{businessId} and (status =2 or status = 3)")
    Double sumIncome(@Param("businessId") Integer businessId, @Param("userId") Integer userId);

    //根据时间范围进行查询益联券
    @Select("select IFNULL(SUM(voucher),0) from tb_order where user_id = #{userId} and DATE_FORMAT(create_time,\"%Y-%m-%d %H:%i:%s\")BETWEEN #{beginTime}and #{endTime} and business_id=#{businessId} and (status =2 or status = 3)")
    Double incomeTime(@Param("beginTime") String beginTime, @Param("endTime") String endTime, @Param("businessId") Integer businessId , @Param("userId") Integer userId);


    @Select("SELECT COALESCE(SUM(voucher_amount), 0) AS total_voucher_amount\n" +
            "FROM tb_voucher_record \n" +
            "WHERE user_id = #{userId} \n" +
            "  AND voucher_type = '1' \n" +
            "  AND DATE(create_time) = CURDATE();\n")
    BigDecimal getTodayMyVoucher(@Param("userId") Integer userId);

    //根据时间范围进行查询订单数
    @Select("select count(*) from tb_order where user_id = #{userId} and DATE_FORMAT(create_time,\"%Y-%m-%d %H:%i:%s\")BETWEEN #{beginTime}and #{endTime} and business_id=#{businessId}")
    Integer orderTime(@Param("beginTime") String beginTime, @Param("endTime") String endTime, @Param("businessId") Integer businessId, @Param("userId") Integer userId);

    @Select("SELECT \n" +
            "    COALESCE(SUM(vr.id), 0) AS total_view_count\n" +
            "FROM \n" +
            "    tb_order vr\n" +
            "LEFT JOIN tb_business tb ON vr.business_id = tb.id\n" +
            "LEFT JOIN tb_user tu ON tb.user_phone = tu.phone\n" +
            "WHERE \n" +
            "    tu.id = #{userId,jdbcType=INTEGER}")
    Integer selectOrderByBusinessId(@Param("userId") Integer userId);

    Integer getCount(Integer businessId);

    /**
     * 不同状态的订单的数量（待收货、待发货、待使用、待评价）
     *
     * @param userId
     * @return
     */
    OrderStatusCountsVO OrderStatusCounts(@Param("userId") Integer userId);

    /**
     * 商家们进三十天益联券的总收入情况、要是传了商家id那就是当前商家进三十天的益联券收入情况
     *
     * @param id
     * @param user
     * @param beginTime
     * @param endTime
     * @return
     */
    List<OrderModel> voucherIncome(@Param("id") Integer id, @Param("user") AdminUserModel user, @Param("beginTime") String beginTime, @Param("endTime") String endTime);

    Double getMonthlyConsumption(@Param("userId")Integer userId);

    @Select("SELECT \n" +
            "    predefined_status.status,\n" +
            "    COALESCE(COUNT(co.id), 0) AS order_count\n" +
            "FROM \n" +
            "    (SELECT 1 AS status UNION ALL SELECT 2 UNION ALL SELECT 3) AS predefined_status\n" +
            "LEFT JOIN   tb_consumption_order co  ON   predefined_status.status = co.status AND co.user_id = #{userId,jdbcType=INTEGER}\n" +
            "GROUP BY   predefined_status.status\n" +
            "ORDER BY   predefined_status.status;")
    List<Map<String,Object>> getConsumptionOrderCounts(@Param("userId") Integer userId);

    @Update("UPDATE tb_order JOIN ( " +
            "    SELECT o.id\n" +
            "    FROM tb_order o\n" +
            "    LEFT JOIN tb_activity a ON o.activity_id = a.id\n" +
            "    WHERE o.status = 1 AND a.classification = 1\n" +
            ") AS to_update ON tb_order.id = to_update.id\n" +
            "SET tb_order.status = 4, tb_order.order_reason = 79;")
    void updateOrderToday();
}
